package cn.topcodes.unicorn.regionspider;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws IOException {
        StringBuilder sql = new StringBuilder();
        long id = 0, provinceId = 0, cityId = 0;

        Connection connection = Jsoup.connect("http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html");
        Document document = connection.get();
        Elements elements = document.getElementsByClass("TRS_PreAppend");
        for(Element element : elements) {
            Elements regions = element.children();
            //  打印html
            //  System.out.println(regions);
            //  遍历每个地区项
            for(Element e : regions) {
                id ++;

                //  通过大写判断是否省份
                boolean isProvince = e.html().contains("<b>");
                if(isProvince) {
                    String no = e.getElementsByAttributeValue("lang", "EN-US").get(0).text().replace(" ","")
                            .replace("<span>&nbsp;&nbsp;&nbsp;&nbsp; </span>","").trim();
                    String provinceName = e.getElementsByAttributeValue("style", "font-family: 宋体").get(0).text().replace("　","").trim();
                    System.out.println("省份\t" + no + "\t" + provinceName);

                    provinceId = id;
                    sql.append("insert t_core_region(id,name,code,parent_id) values(" + id + ",\"" + provinceName + "\",\"" + no + "\",null);\n");
                }else {
                    Element no = e.getElementsByAttributeValue("lang", "EN-US").get(0);
                    //  通过八个空格判断是否城市
                    boolean isCity = no.html().contains("<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>");
                    if(isCity) {
                        String noStr = no.text().replace(" ","").trim();
                        String city = e.getElementsByAttributeValue("style", "font-family: 宋体").get(1).text().replace("　","").trim();
                        System.out.println("\t城市\t" + noStr + "\t" + city);

                        cityId = id;
                        sql.append("insert t_core_region(id,name,code,parent_id) values(" + id + ",\"" + city + "\",\"" + noStr + "\"," + provinceId + ");\n");
                    }else {
                        String noStr = no.text().replace("<span>&nbsp;&nbsp;&nbsp;&nbsp; </span>","").replace(" ","").trim();
                        String area = e.getElementsByAttributeValue("style", "font-family: 宋体").get(1).text().replace("　","").trim();
                        System.out.println("\t\t区/县\t" + noStr + "\t" + area);

                        sql.append("insert t_core_region(id,name,code,parent_id) values(" + id + ",\"" + area + "\",\"" + noStr + "\"," + cityId + ");\n");
                    }
                }
            }
        }

        //  写入文件
        File sqlFile = new File("regions.sql");
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sqlFile)));
        writer.write(sql.toString());
        writer.close();
    }
}
